/**
 * 
 */
package com.xbang.core.dao.search.component;

import java.io.Serializable;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;

import com.xbang.core.utils.SearchUtils;

/**
 * @author dev.zhang
 */
public class SearchMapping implements Serializable {

	private static final long serialVersionUID = 2589975130971178192L;

	private static Map persistentClassCache = new HashMap();

	/**
	 * Get persistent class by alias
	 * 
	 * @param alias
	 * @return
	 */
	public static Class getPersistentClass(String alias) {
		return (Class) persistentClassCache.get(alias);
	}

	/**
	 * 
	 * @param keySet
	 */
	public static void init(Set keySet) {
		for (Iterator iter = keySet.iterator(); iter.hasNext();) {

			try {
				Class persistentClass = Class.forName((String) iter.next());
				persistentClassCache.put(SearchUtils.getAlias(persistentClass), persistentClass);
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}

		}
	}

	/**
	 * 
	 * @param filePath
	 */
	public void setConfigLocation(String filePath) {
		ResourceBundle bundle = ResourceBundle.getBundle(filePath);
		for (Enumeration keys = bundle.getKeys(); keys.hasMoreElements();) {
			String key = (String) keys.nextElement();
			Class clazz = null;
			try {
				clazz = Class.forName(bundle.getString(key));
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}

			if (clazz != null) {
				persistentClassCache.put(key, clazz);
			}
		}
	}

}
